package com.mars.upms.web.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mars.base.common.base.BaseController;
import com.mars.base.common.base.BaseResult;
import com.mars.base.common.exception.AppException;
import com.mars.base.common.utils.Paginator;
import com.mars.upms.dto.UpmsRoleDto;
import com.mars.upms.entity.UpmsRole;
import com.mars.upms.rpc.api.UpmsRoleService;

@RestController
@RequestMapping(value = "/api/upms/role")
public class UpmsRoleController extends BaseController {

	@Autowired
	private UpmsRoleService upmsRoleService;

	@PostMapping(value = "/add")
	public Object add(@RequestBody UpmsRole entity) {
		try {
			if (StringUtils.isBlank(entity.getName())) {
				return BaseResult.failure("角色名称不能为空");
			}
			this.upmsRoleService.insertSelective(entity);

			return BaseResult.ok();
		} catch (AppException e) {
			throw e;
		}
	}

	@GetMapping(value = "/delete/{id}")
	public Object delete(@PathVariable Integer id) {
		try {
			this.upmsRoleService.delete(id);
			return BaseResult.ok();
		} catch (AppException e) {
			throw e;
		}
	}

	@PostMapping(value = "/update")
	public Object update(@RequestBody UpmsRole entity) {
		try {
			if (StringUtils.isBlank(entity.getName())) {
				return BaseResult.failure("角色名称不能为空");
			}
			this.upmsRoleService.updateSelective(entity);

			return BaseResult.ok();
		} catch (AppException e) {
			throw e;
		}
	}

	@GetMapping(value = "/get/{id}")
	public Object get(@PathVariable Integer id) {
		return this.upmsRoleService.selectById(id);
	}
	
	@GetMapping(value = "/list")
	public Object list(UpmsRole entity) {
		return this.upmsRoleService.selectPage(new Paginator(this.request), entity);
	}

	/**
	 * 分配权限到角色
	 * @param dto
	 * @return
	 */
	@PostMapping(value = "/assignPermission")
	public Object assignPermission(@RequestBody UpmsRoleDto dto) {
		try {
			UpmsRole selectById = this.upmsRoleService.selectById(dto.getId()) ;
			if(null == selectById) {
				return BaseResult.failure("该角色不存在");
			}
			this.upmsRoleService.assignPermission(dto);
			return BaseResult.ok();
		} catch (AppException e) {
			throw e;
		}
	}
	
	@GetMapping(value = "/getRolePermissionAssign")
	public Object getRolePermissionAssign(UpmsRole entity) {
		UpmsRole selectById = this.upmsRoleService.selectById(entity.getId()) ;
		if(null == selectById) {
			return BaseResult.failure("该角色不存在");
		}
		return this.upmsRoleService.getRolePermissionAssign(entity.getId()) ;
	}
	
	/**
	 * 将用从角色中移除
	 * @param roleUserAssignId
	 * @return
	 */
	@GetMapping(value = "/remove_role_user")
	public Object removeRoleUser(Integer roleUserAssignId) {
		try {
			this.upmsRoleService.removeRoleUser(roleUserAssignId);
			return BaseResult.ok();
		} catch (AppException e) {
			throw e;
		}
	}
}
